"""Setup the hrms application"""
import logging

import pylons.test

from chowa.lib.site.auth import add_permissions, add_groups

from hrms.config.environment import load_environment
from hrms.model.meta import Session, Base

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup hrms here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    Base.metadata.create_all(bind=Session.bind)
    
    # Add permissions and groups
    admin_perms = [
        'hrms.administer.awards',
        'hrms.administer.orders',
        'hrms.administer.appointments',
        'hrms.administer.education',
        'hrms.administer.holidays']
    hr_perms = [
        'hrms.add.employee',
        'hrms.edit.employee']
    
    add_permissions(admin_perms, Session)
    add_groups([
        ('hrms.administrators', admin_perms),
        ('hrms.hr', hr_perms),
    ], Session)
